package com.hr.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import com.hr.model.UserModel;

public class SqlUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		UserModel u = new UserModel();
		Map<String ,Object> map = generateUpdateModelInfo(u,"user_id");
		System.out.println(map.get("fields"));
	}
	
	/**
	 * 构造model类中insert sql参数
	 * 返回结果：
	 * eg:
	 * <p> fieldLength:3 </p>
	 * <p>fields:user_id,name,department_code </p>
	 * <p>fieldParams:new Object[]{001,'admin','003'} </p>
	 * @param model 实体类
	 * @return
	 */
	public static <T> Map<String ,Object> generateInsertModelInfo(T model){
		Map<String ,Object> map = new HashMap<String ,Object>();
		StringBuffer attrs = new StringBuffer();
		
		Field[] fields = model.getClass().getDeclaredFields();
		Object[] params = new Object[fields.length];
		String fieldName;
		Method method ;
		for(int i=0;i<fields.length;i++){
			fieldName = fields[i].getName();
			attrs.append(fieldName+",");
			Object result = null;
			try {
				method = model.getClass().getMethod("get"+firstUpperCase(fieldName));
				result = (Object) method.invoke(model);
			}catch (Exception e) {
				e.printStackTrace();
			}
			params[i] = result;
		}
		attrs.setLength(attrs.length()-1);
		map.put("fieldLength", fields.length);
		map.put("fields", attrs.toString());
		map.put("fieldParams", params);
		map.put("preValues", generatePreValues(fields.length));
		return map;
	}
	/**
	 * 构造model类中update sql参数
	 * 返回结果：
	 * eg:
	 * <p>fields:user_id=:user_id,name=:name </p>
	 * @param model 实体类
	 * @param exceptFiled 剔除字段
	 * @return
	 */
	public static <T> Map<String ,Object> generateUpdateModelInfo(T model,String...exceptFiled){
		Map<String ,Object> map = new HashMap<String ,Object>();
		StringBuffer attrs = new StringBuffer();
		
		Field[] fields = model.getClass().getDeclaredFields();
		String fieldName;
		int l = exceptFiled==null?0:exceptFiled.length;
		Object[] params = new Object[fields.length-l];
		Method method ;
		boolean excetFlag = false;
		int index= 0;
		for(int i=0;i<fields.length;i++){
			fieldName = fields[i].getName();
			//检查是否是剔除字段
			for(String ef:exceptFiled){
				if(ef.equals(fieldName)){
					excetFlag = true;
					break;
				}
			}
			//如果是剔除字段，则跳过
			if(excetFlag){
				excetFlag = false;
				continue;
			}
			attrs.append(fieldName+"=?,");
			Object result = null;
			try {
				method = model.getClass().getMethod("get"+firstUpperCase(fieldName));
				result = (Object) method.invoke(model);
			}catch (Exception e) {
				e.printStackTrace();
			}
			params[index] = result;
			index++;
		}
		attrs.setLength(attrs.length()-1);
		map.put("fields", attrs.toString());
		map.put("fieldParams", params);
		return map;
	}
	/**
	 * 构造预定义SQL
	 * eg: ?,?,?
	 * @param num
	 * @return
	 */
	private static String generatePreValues(int num){
		StringBuffer preValues = new StringBuffer();
		for(int i=0;i<num;i++){
			preValues.append("?,");
		}
		preValues.setLength(preValues.length()-1);
		return preValues.toString();
		
	}
	
	private static String firstUpperCase(String value){
		value = value.substring(0, 1).toUpperCase()+value.substring(1, value.length());
		return value;
	}

}
